/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Login;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 *
 * @author Bharath
 */
public class LoginServlet extends HttpServlet {

    private String query;
    private PreparedStatement pstmt;
    private ResultSet rs;
    MySqlConnector ms = new MySqlConnector();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            System.out.println("*************************");
            String memid = request.getParameter("memberid");
            String password = request.getParameter("password");
            System.out.println("member id is ......." + memid);
            System.out.println("password is ......." + password);
            ms.Connect();
            query = "select memberid , password , status from registration where memberid = '" + memid + "' and password = '"+password+"'and status = '1'; ";
            System.out.println("query :  "+query);
            //Statement
            pstmt = ms.con.prepareStatement(query);
            //Result Set
            rs = pstmt.executeQuery();
            System.out.println("ResultSet rs "+rs);
            if (rs.next() == true)
            {
                //set sessions
                HttpSession hs = request.getSession(true);
                hs.setAttribute("memid", rs.getString(1));
                hs.setAttribute("password", rs.getString(2));
                System.out.println("MEMBER ID from the resultset is " + rs.getString(1));
                System.out.println("PASSWORD from the resultset is " + rs.getString(2));
                
                if (memid.equals("SCFAdmin"))
                {
                     getServletContext().getRequestDispatcher("/AdminAccount.jsp").forward(request, response);
                }
                else
                {
                     getServletContext().getRequestDispatcher("/UserAccount.jsp").forward(request, response);
                }
            }
            else
            {
                getServletContext().getRequestDispatcher("/errorlogin.jsp").forward(request, response);
                System.out.print("Login is failed so redirecting to error login page");
            }
            
            
        } catch (Exception ex) {
            Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }

    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
